/**
 * 
 */
package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wf.captcha.utils.CaptchaUtil;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

/**
 * @author 030998
 *
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaController {
	
	private ExpiringMap<String,String> expir = ExpiringMap.builder().maxSize(1000000).expiration(10, TimeUnit.MINUTES)
			.expirationPolicy(ExpirationPolicy.ACCESSED).variableExpiration().build();
	
	@GetMapping("/list")
    public String index() {
        return "/modules/captcha/list2";
    }

	@RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 设置位数
        //CaptchaUtil.out(5, request, response);
        // 设置宽、高、位数
        CaptchaUtil.out(130, 48, 5, request, response);
        
        // 使用gif验证码
//        GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
//        CaptchaUtil.out(gifCaptcha, request, response);
    }
	
	@PostMapping("/login")
	@ResponseBody
    public Map<String, String> login(HttpServletRequest request, String username,String password,String verCode){
		Map<String, String> map = new HashMap<>();
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            System.out.println("验证码不正确");
            map.put("error", "验证码不正确");
            return map;
        }
        System.out.println("验证码正确");
        map.put("ok", "验证码正确");
        
        
        if(expir.containsKey("")) {
        	
        } else {
        	expir.put("", "");
        }
        
        return map;
    } 
}
